package com.csproj.basic.repository;

import com.csproj.basic.domain.ElectronicLock;
import com.csproj.basic.domain.FixedReadingDevice;
import com.csproj.basic.repository.custom.ElectronicLockRepositoryCustom;
import org.springframework.stereotype.Repository;

import org.springframework.data.jpa.repository.*;

import java.util.List;


/**
 * Spring Data JPA repository for the ElectronicLock entity.
 */
@SuppressWarnings("unused")
@Repository
public interface ElectronicLockRepository extends JpaRepository<ElectronicLock, String> , ElectronicLockRepositoryCustom {
    /**
     * 根据ids查询固定读写设备列表信息
     * @author LM
     * @date   2018-09-14
     * @param ids
     * @return
     */
    @Query(" from ElectronicLock where deletedFlag = 0 and id in (?1) ")
    List<ElectronicLock> findByIds(String[] ids);


    /**
     * 根据ID删除数据
     * @author LM
     * @date   2018-09-14
     * @param id 设备id
     */
    @Modifying
    @Query("UPDATE ElectronicLock SET enableFlag = ?2 where id = ?1")
    void updateStatus(String id, boolean enable);

    /**
     * 根据ID删除数据
     * @author LM
     * @date   2018-09-14
     * @param id 机构id
     */
    @Query(" from ElectronicLock where deletedFlag = 0 and id = ?1")
    ElectronicLock findById(String id);

    /**
     * 根据机构ids，批量固定读写设备信息
     * @param ids
     */
    @Modifying
    @Query(" update ElectronicLock set deletedFlag = 1 where id in (?1) ")
    void deleteIds(String[] ids);

    /**
     * 获取未分配数量
     * @return
     */
    @Query( " FROM ElectronicLock  where (siteId = '' or siteId is null) and deletedFlag = 0 ")
    List<ElectronicLock> findByUnAlloat();
    ElectronicLock findFirstByLockNoAndDeletedFlagIsFalse(String lockNo);

    /**
     * 根据状态查询数量
     * @param statusUsed
     * @return
     */
    @Query( " FROM ElectronicLock  where state = ?1  and deletedFlag = 0 " )
    List<ElectronicLock> findByStatus(String statusUsed);

    @Query( " FROM ElectronicLock  where deletedFlag = 0 and siteId = ?1 and enableFlag = ?2  " )
    List<ElectronicLock> findBySiteIdAndEnbleFlag(String siteId,Boolean flag);

    @Query( " from ElectronicLock  where deletedFlag = 0 " )
    List<ElectronicLock> findAll();

    @Query( " from ElectronicLock el where el.deletedFlag = 0 and el.siteId=?1 and el.lockNo in (select dp.lockCode from Dispatch dp where dp.deletionTime is null and dp.arriveTime is null)" )
    List<ElectronicLock> findBySiteIdAndMessage(String siteId);

    @Query( " from ElectronicLock el where el.deletedFlag = 0 and el.lockNo in (select dp.lockCode from Dispatch dp where dp.deletionTime is null and dp.arriveTime is null)" )
    List<ElectronicLock> findAllSiteIdAndMessage();


    @Query( " FROM ElectronicLock  where state = ?1  and deletedFlag = 0 and  siteId = ?2 and  enableFlag = ?3 " )
    List<ElectronicLock> findByStatusAndSiteIdAndEnbaleFlag(String statusUsed, String siteId, boolean b);

    ElectronicLock findFirstByLockNoAndEnableFlagTrueAndDeletionTimeIsNull(String lockNo);
}
